# coding: utf-8

import numpy as np
import pandas as pd

dpath = '../data/'
train = pd.read_csv(dpath + "Otto_train.csv")
print(train.head())
print(train.info())
print(train.describe())

# Target 分布，看看各类样本分布是否均衡
from matplotlib import pyplot
import seaborn as sns

sns.countplot(train.target);
pyplot.xlabel('target');
pyplot.ylabel('Number of occurrences');

# 将类别字符串变成数字
# drop ids and get labels
y_train = train['target']  # 形式为Class_x
y_train = y_train.map(lambda s: s[6:])
y_train = y_train.map(lambda s: int(s) - 1)
train = train.drop(["id", "target"], axis=1)
X_train = np.array(train)


def ss(X_train):
    from sklearn.preprocessing import StandardScaler
    # 初始化特征的标准化器
    ss_X = StandardScaler()
    # 分别对训练和测试数据的特征进行标准化处理
    X_train = ss_X.fit_transform(X_train)
    print(X_train[0])
    return X_train


def lr_fit(X_train, y_train):
    from sklearn.model_selection import GridSearchCV
    from sklearn.linear_model import LogisticRegression

    # 需要调优的参数
    # 请尝试将L1正则和L2正则分开，并配合合适的优化求解算法（slover）
    # tuned_parameters = {'penalty':['l1','l2'],
    #                   'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000]
    #                   }
    penaltys = ['l1', 'l2']
    Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
    tuned_parameters = dict(penalty=penaltys, C=Cs)

    lr_penalty = LogisticRegression()
    grid = GridSearchCV(lr_penalty, tuned_parameters, cv=5, scoring='neg_log_loss')
    grid.fit(X_train, y_train)

    print(grid.best_params_)
    print(grid.best_score_)

    LogisticRegression()

    return grid


def save_model(lr, path):
    from sklearn.externals import joblib
    joblib.dump(lr, path)


def load_model(path):
    from sklearn.externals import joblib
    return joblib.load(path)


def model_exist(path):
    import os
    return os.path.exists(path)


path = '../dist/lr2.pkl'
if model_exist(path):
    print("load model from ", path)
    lr = load_model(path)
else:
    print("start fit")
    lr = lr_fit(X_train, y_train)
    save_model(lr, path)

y_pre = lr.predict(X_train)
import sklearn.metrics as metrics

print("Classification report for classifier: ", metrics.classification_report(y_train, y_pre))
print("Confusion matrix: ", metrics.confusion_matrix(y_train, y_pre))
print("accuracy_score: ", metrics.accuracy_score(y_train, y_pre))
